package com.chinatsp.onlineapp.ui.weather;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import tsp.cn.http.tools.LogUtil;
import tsp.cn.http.tools.PreferenceUtils;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.chinatsp.onlineapp.R;
import com.chinatsp.onlineapp.base.Constant;
import com.chinatsp.onlineapp.bean.CityList;

public class ItemCitySelectAdapter extends BaseAdapter {

	private Context mContext;
	private ViewGroup root;
	private List<CityList> list_city;
//	private CheckBox cb_select;
//	private TextView tv_city;
//	private ImageView iv_position;
	private String weatherDefaultCity = PreferenceUtils.getPreference(Constant.DEFAULT_CITY);
	private String weather_city1="#";
	private String weather_city2="#";
	private String citys[];
	private Set cityName;

	private int DefaultPosition = 0;
	private LinkedList<Integer> list_selectedCity = new LinkedList<Integer>();
	

	public ItemCitySelectAdapter(Context context, List<CityList> cityLists) {
		mContext = context;
		list_city = cityLists;
		weatherDefaultCity = PreferenceUtils.getPreference(Constant.DEFAULT_CITY,weatherDefaultCity);
		weather_city1 = PreferenceUtils.getPreference(Constant.WEATHER_CITY1, weather_city1);
		weather_city2 = PreferenceUtils.getPreference(Constant.WEATHER_CITY2, weather_city2);
		citys =new String[]{weather_city1,weather_city2};
			
	}

	@Override
	public int getCount() {
		return list_city.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}
	class Holder{
		ViewGroup root;
		CheckBox cb;
		TextView tv_city;
		ImageView iv;
	}
	Holder holder ;
	String whichCity;
	
	@Override
	public View getView(final int position, View convertview, ViewGroup parent) {
		 holder = new Holder();
		if (convertview == null) {
			convertview = LayoutInflater.from(mContext).inflate(R.layout.item_gv_city_select, null,false);
			holder.root = (ViewGroup) convertview;
			holder.cb = (CheckBox) convertview.findViewById(R.id.cb_select);
			holder.tv_city = (TextView) convertview.findViewById(R.id.tv_city);
			holder.iv = (ImageView) convertview.findViewById(R.id.iv_position);
			convertview.setTag(holder);
		}else{
			holder = (Holder) convertview.getTag();
		}
		
	

		holder.tv_city.setText(list_city.get(position).getName());
		
		String itemCityName =holder.tv_city.getText().toString();		
//		Log.e("onclick","---onClick"+position+"  itemCityName : "+itemCityName);
		
		
		setLocalIcon(holder.root, weatherDefaultCity);
		setCitySelect(holder.root);
		String temp ="";
		int select;
		holder.root.setClickable(true);
		holder.root.setFocusable(true);
		holder.root.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ViewGroup root = (ViewGroup) v;
				final String itemCityName = ((TextView)root.getChildAt(1)).getText().toString();
				

				weather_city1 = PreferenceUtils.getPreference(Constant.WEATHER_CITY1, weather_city1);
				weather_city2 = PreferenceUtils.getPreference(Constant.WEATHER_CITY2, weather_city2);
				
				if (itemCityName.contains(weatherDefaultCity)) {					
					return;
				}
				else if (weather_city1.contains(itemCityName)) {
					PreferenceUtils.savePreference(Constant.WEATHER_CITY1, "#");
				}
				else if (weather_city2.contains(itemCityName)) {
					PreferenceUtils.savePreference(Constant.WEATHER_CITY2, "#");
					
				}else if(!weather_city1.contains(itemCityName) && weather_city1.equals("#")){
					PreferenceUtils.savePreference(Constant.WEATHER_CITY1, itemCityName);
					
				}else if(!weather_city2.contains(itemCityName) && weather_city2.equals("#")){
					PreferenceUtils.savePreference(Constant.WEATHER_CITY2, itemCityName);
				}else{

					String title = mContext
							.getString(R.string.select_city_dialog_title);
					citys = new String[]{weather_city1,weather_city2};
					
					new AlertDialog.Builder(mContext)
					.setTitle(title)
					.setSingleChoiceItems(citys, -1, new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface arg0, int which) {
							whichCity = citys[which];
							
						}
					})
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(
										DialogInterface arg0, int arg1) {
									
									if (whichCity .contains(weather_city1)) {
										PreferenceUtils.savePreference(Constant.WEATHER_CITY1, itemCityName);
									}else if(whichCity .contains(weather_city2)){
										PreferenceUtils.savePreference(Constant.WEATHER_CITY2, itemCityName);
									}
									weather_city1 = PreferenceUtils.getPreference(Constant.WEATHER_CITY1, weather_city1);
									weather_city2 = PreferenceUtils.getPreference(Constant.WEATHER_CITY2, weather_city2);
									LogUtil.e(whichCity);
									
									
									notifyDataSetChanged();
									

								}
							}).setNegativeButton("取消", null).show();
		
				}
				notifyDataSetChanged();
			}
		});
		//

		return convertview;
	}

	/**
	 * 设置默认城市icon
	 * 
	 * @param v
	 *            iv_icon
	 * @param position
	 */

	private void setLocalIcon(ViewGroup root, String weatherDefaultCity) {
		weather_city1 = PreferenceUtils.getPreference(Constant.WEATHER_CITY1, weather_city1);
		weather_city2 = PreferenceUtils.getPreference(Constant.WEATHER_CITY2, weather_city2);
		String cityName = ((TextView)root.getChildAt(1)).getText().toString();
		if (cityName.contains(weatherDefaultCity)) {
			root.getChildAt(2).setVisibility(View.VISIBLE);
			root.getChildAt(0).setSelected(true);
		}
	}

	/**
	 * 设置选中的城市
	 * 
	 * @param citys
	 * @param iv
	 * @param position
	 */
	private void setCitySelect(ViewGroup root) {
		String itemCityName =((TextView) root.getChildAt(1)).getText().toString();
		if (itemCityName.contains(weather_city1)||itemCityName.contains(weather_city2)||itemCityName.contains(weatherDefaultCity)) {
			root.getChildAt(0).setSelected(true);
		}else{
			root.getChildAt(0).setSelected(false);
		}
		notifyDataSetChanged();		
		
	}

	/**
	 * 条目的点击事件
	 * 
	 * @param position
	 */
	private void itemClickSelectCity(int position) {

//		if (position == DefaultPosition) {
//			T.show(mContext, "定位位置，不能取消选中", 0);
//		} else {
//			if (list_selectedCity.contains(position)) {
//				list_selectedCity.remove(new Integer(position));
//				setCitySelect(cb_select, position);
//				for (int i = 0; i < list_selectedCity.size(); i++) {
//					System.out.println(list_selectedCity.get(i));
//				}
//			} else {
//				if (list_selectedCity.size() < 3) {
//					list_selectedCity.add(new Integer(position));
//					setCitySelect(cb_select, position);
//					for (int i = 0; i < list_selectedCity.size(); i++) {
//						System.out.println(list_selectedCity.get(i));
//					}
//				} else {
//					String title = mContext
//							.getString(R.string.select_city_dialog_title);
//					new AlertDialog.Builder(mContext)
//							.setTitle(title)
//							.setSingleChoiceItems(citys, 1, null)
//							.setPositiveButton("确定",
//									new DialogInterface.OnClickListener() {
//										@Override
//										public void onClick(
//												DialogInterface arg0, int which) {
//
//										}
//									}).setNegativeButton("取消", null).show();
//				}
//			}
//		}
	}

}
